
#include "TestApp.h"

CTestApp::CTestApp()
{
	m_hInst = NULL;
	m_hWnd = NULL;

	m_pIndexBuffer = NULL;
	m_pVertexBuffer = NULL;
	m_pRenderSystem = NULL;
}


CTestApp::~CTestApp()
{

}


void CTestApp::onEvent(MSG& msg)
{

}


bool CTestApp::updateFrame(int passedTime)
{
	//Render();
	//static int FPS = 0;
	//static int tempFPSTime = 0;
	//FPS++;
	//int nowtime = GetTickCount();
	//int CurFPS = 0;
	//if (nowtime - tempFPSTime >= 1000)
	//{
	//	CurFPS = FPS;
	//	tempFPSTime = nowtime;
	//	FPS = 0;
	//	printf("--->FPS %d\n", CurFPS);

	//}
	m_pTexMgr->update( passedTime , passedTime );
	CSampleApp::updateFrame(passedTime);
	return true;
}

bool CTestApp::initApp( const char* appModuleFile )
{
	m_pTexMgr = new LaborBaseTextureMgr(L"BaseTexture" , 1 );
	m_pTexMgr->setThis(m_pTexMgr);
	//m_TextureMagrs.push_back(pMgr);
	m_pTexMgr->setMaxSize(256 * 1024 * 1024);

	m_pTexMgr->addPath( L"E:\\svn_Depot\\Labor-3D\\trunk\\bin\\resource\\texture\\");
	return CSampleApp::init( appModuleFile, L"Test02_3DRender", L"Test02 : 3DRender", 800, 600 );
}

void CTestApp::initRenderData()
{
	m_pRenderSystem->setInputLayout( IL_PT );


	ILGpuProgram *pGpuProgram = m_pRenderSystem->createGPUProgram();
	wstring strVS, strPS;
	strVS = m_strShaderPath;
	strPS = m_strShaderPath;

	strVS += L"vertexShader.hlsl";
	strPS += L"pixelShader.hlsl";

	pGpuProgram->load( strVS.c_str(), strPS.c_str(), NULL, 0);
	m_pRenderSystem->setGPUProgram( pGpuProgram );
	HRESULT hr = S_OK;
	// Create vertex buffer


	SimpleVertex vertices[] =
	{
		{ D3DXVECTOR4( -1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( -1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

		{ D3DXVECTOR4( -1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( -1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

		{ D3DXVECTOR4( -1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( -1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( -1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( -1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

		{ D3DXVECTOR4( 1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

		{ D3DXVECTOR4( -1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, -1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( -1.0f, 1.0f, -1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },

		{ D3DXVECTOR4( -1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, -1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 0.0f ) },
		{ D3DXVECTOR4( 1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 1.0f, 1.0f ) },
		{ D3DXVECTOR4( -1.0f, 1.0f, 1.0f, 1.0f ), D3DXVECTOR2( 0.0f, 1.0f ) },
	};

	SimpleVertex *pVertex = NULL;
	int vertexNumber = 24;
	m_pVertexBuffer = m_pRenderSystem->createVertexBuffer( sizeof(SimpleVertex), vertexNumber, HBU_STATIC);
	pVertex = (SimpleVertex*)m_pVertexBuffer->lock( 0, vertexNumber, eHBL_WRITE_DISCARD);
	for ( int i=0; i< vertexNumber; i++ )
	{
		*pVertex = vertices[i];
		pVertex++;
	}
	m_pVertexBuffer->unlock();


	DWORD indices[] =
	{
		3,1,0,
		2,1,3,

		6,4,5,
		7,4,6,

		11,9,8,
		10,9,11,

		14,12,13,
		15,12,14,

		19,17,16,
		18,17,19,

		22,20,21,
		23,20,22
	};


	m_pIndexBuffer  = m_pRenderSystem->createIndexBuffer( IT_32BIT, 36, HBU_WRITE_ONLY);

	int *pIndex = NULL;
	pIndex = static_cast<int*>(m_pIndexBuffer->lock(0, 36*sizeof(int), eHBL_WRITE_DISCARD));

	for ( int i=0; i< 36; i++ )
	{
		*pIndex = indices[i];
		pIndex++;
	}
	m_pIndexBuffer->unlock();
	//// Set vertex buffer
	UINT stride = sizeof( SimpleVertex );
	UINT offset = 0;
	m_pRenderSystem->setVertexBuffer( 0,1, m_pVertexBuffer, &stride, &offset);
	m_pRenderSystem->setIndexBuffer( m_pIndexBuffer, IT_32BIT );
	m_pRenderSystem->setPrimitiveTopology( eL_PT_TRIANGLELIST );


	// Initialize the world matrix
	D3DXMatrixIdentity( &m_World );

	// Initialize the view matrix
	D3DXVECTOR3 Eye( 0.0f, 0.0f, -6.0f );
	D3DXVECTOR3 At( 0.0f, 0.0f, 0.0f );
	D3DXVECTOR3 Up( 0.0f, 1.0f, 0.0f );
	m_Camera.setPosition( CLPoint3(0.0f, 0.0f, -6.0f) );
	m_Camera.lookAt( CLPoint3(0.0f, 0.0f, 0.0f) );


	//m_Camera.
	D3DXMatrixLookAtLH( &m_View, &Eye, &At, &Up );

	RECT rc;
	GetClientRect( m_hWnd, &rc );
	UINT width = rc.right - rc.left;
	UINT height = rc.bottom - rc.top;

	//// Initialize the projection matrix
	D3DXMatrixPerspectiveFovLH( &m_Projection, ( float )D3DX_PI * 0.25f, width / ( FLOAT )height, 0.1f, 3000.0f );

	//m_pTexture = m_pRenderSystem->createTextureFromFile(L"seafloor.dds");
	//m_pRenderSystem->setTexture( m_pTexture );

	m_hTexture = m_pTexMgr->add( L"seafloor.dds", 0 , true );
	//// sample state 
	sLSamplerStateDesc _sampler_desc;
	ILSamplerState *pSamplerState = m_pRenderSystem->createSamplerState( _sampler_desc );
	m_pRenderSystem->setSamplerState( 0, pSamplerState );


	pGpuProgram->commit();
	m_pRenderSystem->setWorldMatrix( (float* )&m_World );
	CLMatrix4x4 viewMat = m_Camera.getViewMatrix();
	CLMatrix4x4 proMat  = m_Camera.getProjectionMatrix();

	//m_pRenderSystem->setViewMatrix( (float* )&m_View );
	//m_pRenderSystem->setProMatrix( (float* )&m_Projection );

	m_pRenderSystem->setViewMatrix( (float* )&viewMat );
	m_pRenderSystem->setProMatrix( (float* )&proMat );

}


//--------------------------------------------------------------------------------------
// Render the frame
//--------------------------------------------------------------------------------------
void CTestApp::Render()
{
	if ( NULL == m_pRenderSystem )
		return;
	// Update our time
	static float t = 0.0f;

	static DWORD dwTimeStart = 0;
	DWORD dwTimeCur = GetTickCount();
	if( dwTimeStart == 0 )
		dwTimeStart = dwTimeCur;
	t = ( dwTimeCur - dwTimeStart ) / 1000.0f;

	CLTexture *pTexture = NULL;
	pTexture = (CLTexture *)m_hTexture.getResource();
	m_pRenderSystem->setTexture( pTexture );

	//Animate the cube

	D3DXMatrixRotationY( &m_World, t );

	m_pRenderSystem->clearBackBuffer(0.0f, 0.125f, 0.3f, 1.0f);
	m_pRenderSystem->setWorldMatrix( (float* )&m_World );


	m_pRenderSystem->begin();
	UINT stride = sizeof( SimpleVertex );
	UINT offset = 0;
	m_pRenderSystem->setVertexBuffer( 0,1, m_pVertexBuffer, &stride, &offset);
	m_pRenderSystem->setIndexBuffer( m_pIndexBuffer, IT_32BIT );
	m_pRenderSystem->setPrimitiveTopology( eL_PT_TRIANGLELIST );

	//for ( int i=0; i<1000; i++ )
		m_pRenderSystem->drawIndexed( 36, 0, 0 );
	m_pRenderSystem->present(0, 0);
	m_pRenderSystem->end();
}
